//
//  UserInfoCell.m
//  Wujin
//
//  Created by 东 on 16/12/11.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "UserInfoCell.h"

@implementation UserInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    _nameTF.text = [WJUser user].name;
    _phoneTF.text = [WJUser user].phone;
    _addressTF.text = [WJUser user].address;
    
    _date = [NSDate date];
    _dateLab.text = [dbase.dateFormatter stringFromDate:_date];
}

- (void)setTotalPrice:(CGFloat)totalPrice
{
    _totalPrice = totalPrice;
    
    _totalPriceLab.text = F(@"%.2lf元", _totalPrice);
}

- (IBAction)makeOrder:(id)sender
{
    
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *orderId = [formatter stringFromDate:_date];
    NSArray *ordergoodsArray = [OrderGoodsModel mj_keyValuesArrayWithObjectArray:_dataArray];
    NSDictionary *dict = @{
                           @"Order_address" : _addressTF.text,
                           @"Order_id" : orderId,
                           @"Order_user_id" : [WJUser user].user_id,
                           @"Order_user_phone" : [WJUser user].phone,
                           @"Order_name" : _nameTF.text,
                           @"Order_phone" : _phoneTF.text,
                           @"Order_time" : _dateLab.text,
                           @"Order_totalPrice" : F(@"%lf", _totalPrice),
                           @"Order_words" : _liuyanTF.text,
                           @"Order_orderGoodses" : ordergoodsArray,
                           };
    
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSDictionary *orderPara = @{
                                
                                @"data" : str
                                };
    NSString *url = F(@"%@Index/ajaxOrder", BASEURL);
    [FSServer requestWithTarget:url param:orderPara result:^(id responseObject, NSError *error) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *dict = responseObject;
            if ([dict[@"flag"] intValue] == 1)
            {
                for (OrderGoodsModel *model in self.dataArray)
                {
                    if (model.isDelete)
                    {
                        BOOL success = [dbase deleteForTable:DB_GOODS where:@"Gid=%@", model.Gid];
                        DLog(@"success===%d", success);
                    }
                }
                [MRHud show];
                [MRHud dismissWithSuccess:@"提交成功"];
                if (_block) {
                    _block();
                }
            }
        }
    }];
}


@end
